/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-07-01
 * Time: 11:11
 */
class Solution23 {
    int m,n;
    int[] dx = {1,-1,0,0};
    int[] dy = {0,0,1,-1};
    public void solve(char[][] board) {
        m = board.length;
        n = board[0].length;

        //1.先把边界处理
        for(int i = 0; i < m; i++) {
            if(board[i][0] == 'O') {
                dfs(board,i,0);
            }
            if(board[i][n-1] == 'O') {
                dfs(board,i,n-1);
            }
        }

        for(int j = 0; j < n; j++) {
            if(board[0][j] == 'O') {
                dfs(board,0,j);
            }
            if(board[m-1][j] == 'O') {
                dfs(board,m-1,j);
            }
        }

        //2.还原
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == 'O') {
                    board[i][j] = 'X';
                }else if(board[i][j] == '.') {
                    board[i][j] = 'O';
                }
            }
        }
    }

    void dfs(char[][] board,int i,int j) {


        board[i][j] = '.';

        for(int k = 0; k < 4; k++) {
            int x = i+dx[k],y = j+ dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {
                dfs(board,x,y);
            }
        }
    }
}
